package com.shareach.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import com.shareach.common.BaseQueryOrder;
import com.shareach.dao.mongo.BaseCityDao;
import com.shareach.domain.BaseCityDomain;
import com.shareach.service.BaseCityService;
import com.shareach.service.exception.BaseService;
import com.shareach.util.ReflectionUtil;

@Service
public class BaseCityServiceImpl extends BaseService<BaseCityDomain> implements BaseCityService {
	@Autowired
	private BaseCityDao baseCityDao;

	@Override
	public synchronized void save(BaseCityDomain domain) {
		domain.setCreatTime(new Date());
		baseCityDao.insert(domain);
	}

	@Override
	public void del(BaseCityDomain domain) {
		if (domain.getCode() == null)
			return;
		Query quyery = new Query();
		quyery.addCriteria(Criteria.where("_id").is(domain.getCode()));
	}

	@Override
	public synchronized void update(BaseCityDomain domain) {
		if (domain.getCode() == null)
			return;
		Long updateId = domain.getCode();
		domain.setCode(null);
		Update update = parseDomaintoUpdate(domain);

		if (update.getUpdateObject().toMap().size() > 0) {
			baseCityDao.update(Criteria.where("_id").is(updateId), update);
		}
	}

	@Override
	public BaseCityDomain getById(Long code) {
		if (code == null)
			return null;
		Query q = new Query();
		q.addCriteria(Criteria.where("_id").is(code));
		return baseCityDao.findOne(q);
	}

	@Override
	public List<BaseCityDomain> getList(BaseCityDomain domain) {
		Query query = parseDomaintoQuery(domain);
		List<BaseCityDomain> baseCityDomains = baseCityDao.findList(query);
		return baseCityDomains;
	}

	@Override
	public List<BaseCityDomain> findAll() {
		return baseCityDao.findAll();
	}

	@Override
	public Page<BaseCityDomain> getPageResult(BaseCityDomain domain, BaseQueryOrder order) {
		Map<Object, Object> domainMap = ReflectionUtil.parseDomaintoMap(domain);
		Query query = new Query();
		for (Entry<Object, Object> entry : domainMap.entrySet()) {
			if (entry.getKey().equals("name")) {
				query.addCriteria(Criteria.where((String) entry.getKey()).regex((String) entry.getValue()));
			} else if (entry.getKey().equals("updateTime")) {
				continue;
			} else {
				query.addCriteria(Criteria.where((String) entry.getKey()).is(entry.getValue()));
			}
		}
		Page<BaseCityDomain> domainPage = baseCityDao.getPageResult(query, order);
		return domainPage;
	}

	@Override
	public synchronized void update(BaseCityDomain domain, BaseCityDomain condition) {
		Query query = parseDomaintoQuery(condition);

		Update update = parseDomaintoUpdate(domain);

		if (update.getUpdateObject().toMap().size() != 0) {
			baseCityDao.update(query, update);
		}
	}

}
